from azs_https import AzsHttpClient
from azs_token_info import TokenInfo


class TokenChatAgent:
    def __init__(self, base_url: str, token: str):
        self.token = token
        self.base_url = base_url

    # 获取当前系统的消耗token 使用情况
    def use_all_token(self):
        url = f'{self.base_url}api/api/v1/dashboard/count-total'
        headers = {"Authorization": self.token}
        try:
            result = {}
            httpclient = AzsHttpClient()
            res = httpclient.get(url=url, headers=headers)
            result['usedToken'] = res['usedToken']
            result['todayUsedToken'] = res['todayUsedToken']
            return result
        except Exception as e:
            print(f"azs_use_token error: {e}")
            return {}

    def get_person_token(self,keyword='zuo'):
        url = f'{self.base_url}api/api/v1/dashboard/user-page'
        headers = {"Authorization": self.token}
        params = {
            "keyword": keyword,
            "pageNum": 1,
            "pageSize": 1
        }
        result = {}
        try:
            httpclient = AzsHttpClient()
            res = httpclient.get(url=url,params=params, headers=headers)
            if len(res['data']) == 1:
                result['usedToken'] = res['data'][0]['usedToken']
                return result
            else:
                return '暂时没有找到该人'

        except Exception as e:
            print(f"azs_person_token error: {e}")
            return {}



if __name__ == '__main__':
    tokenInfo = TokenInfo('http://dev.azures.cn:28888', 'auto_test', 'Qwer123#@!')
    tokenInfo.get_token()
    tokenChatAgent = TokenChatAgent('http://dev.azures.cn:28888/', tokenInfo.token)
    tokenChatAgent.get_person_token()
